package com.lianniu.web.product.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.lianniu.web.operationMessage.service.OperMessageService;
import com.lianniu.web.operationMessage.vo.OperMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.area.service.IAdminAreaService;
import com.lianniu.web.baseService.user.model.Supply;
import com.lianniu.web.baseService.user.service.IFrontUserService;
import com.lianniu.web.baseService.user.service.ISupplyService;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.product.model.ProductCateVo;
import com.lianniu.web.product.model.ProductShopVo;
import com.lianniu.web.product.model.ProductVo;
import com.lianniu.web.product.model.SkuProductVo;
import com.lianniu.web.product.model.SupplyareaVo;
import com.lianniu.web.product.service.IProductCateService;
import com.lianniu.web.product.service.IProductShopService;
import com.lianniu.web.product.service.IProductSpecService;
import com.lianniu.web.product.service.ISkuProductService;
import com.lianniu.web.product.service.ISupplyareaService;
import com.lianniu.web.util.FileUploadUtil;
import com.lianniu.web.util.UUIDUtil;

@Controller
@RequestMapping("/rest/product")
public class SkuProductRestController {

    private static final Logger logger = Logger.getLogger(SkuProductRestController.class);
    @Autowired
    ISkuProductService skuProductService;
    @Autowired
    IProductCateService productCateService;
    @Autowired
    IProductSpecService productSpecService;
    @Autowired
    IProductShopService productShopService;
    @Autowired
    IAdminAreaService adminAreaService;
    @Autowired
    ISupplyService supplyService;
    @Autowired
    ISupplyareaService supplyAreaService;
    @Autowired
    FileUploadUtil fileUpload;
    @Autowired
    IFrontUserService frontUserService;

    @Autowired
    OperMessageService operMessageService;

    /**
     * @Title: skuProductList
     * @Description: 我的商品
     * @author: guoconglin
     * @param request
     * @param skuProduct
     * @return
     * @date: 2016年8月12日 上午9:09:44
     * @return: BaseResult
     */
    @RequestMapping(value = "/skuProductList", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody BaseResult skuProductList(HttpServletRequest request,
                    SkuProductVo skuProduct, Integer currPage, String userid) {
        logger.info("SkuProductRestController  skuProductList begin.");
        try {
            logger.info("skuProductList begin.");
            BaseResult baseResult = skuProductService.selectBySkuProductForPage(skuProduct,
                            currPage, userid);
            logger.info("skuProductList end.");
            logger.info("SkuProductRestController skuProductList end.");
            logger.info("basesResult:" + JSON.toJSONString(baseResult));
            return baseResult;
            // return BaseResult.success();
        } catch (Exception e) {
            logger.error("SkuProductRestController skuProductList error:" + e.getMessage());
            logger.error("SkuProductRestController skuProductList end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: getSkuProductInfo
     * @Description: 商品详情
     * @author: guoconglin
     * @param request
     * @return
     * @date: 2016年8月12日 上午9:10:12
     * @return: BaseResult
     */
    @RequestMapping(value = "/getSkuProductInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody BaseResult getSkuProductInfo(HttpServletRequest request) {
        logger.info("SkuProductRestController  getSkuProductInfo begin.");
        String id = request.getParameter("id");
        try {
            logger.info("getSkuProductInfo begin.");
            BaseResult baseResult = skuProductService.getSkuProductById(id);
            logger.info("getSkuProductInfo end.");
            logger.info("SkuProductRestController getSkuProductInfo end.");
            logger.info("basesResult:" + JSON.toJSONString(baseResult));
            return baseResult;
        } catch (Exception e) {
            logger.error("SkuProductRestController getSkuProductInfo error:" + e.getMessage());
            logger.error("SkuProductRestController getSkuProductInfo end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: dictionaryAdd
     * @Description: 增加商品
     * @author: guoconglin
     * @param request
     * @param skuProductVo
     * @return
     * @date: 2016年8月12日 上午9:10:33
     * @return: BaseResult
     * @throws UnsupportedEncodingException
     */
    @RequestMapping(value = {"/skuProductAdd"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult dictionaryAdd( String userid,
                    String productId, SkuProductVo skuProductVo,String onlinePayType) {
        logger.info("SkuProductRestController skuProductAdd begin.");

        logger.info("skuProductAdd begin.");
        BaseResult baseResult = supplyService.selectByUsersId(userid);
        Supply supply = (Supply) baseResult.getData();
        String supplyId = String.valueOf(supply.getSupplyId());
        String county = supply.getCounty();
        ProductShopVo productShop = new ProductShopVo();
        productShop.setProductId(productId);
        productShop.setSupplyID(supplyId);
        BaseResult bs = productShopService.insertProductShop(productShop);
        String id = productShop.getId();
        try {
            skuProductVo.setAreaId(county);
            skuProductVo.setShopProductId(id);
            skuProductVo.setSaleType("1");
            skuProductVo.setStockNum(skuProductVo.getOverPlusNum());
            skuProductVo.setUnit("头");
            skuProductVo.setApproveStatus("1");
            skuProductVo.setIsPayOnline(onlinePayType);
            skuProductService.insertSkuProduct(skuProductVo);

            try {
                OperMessage operMessage = new OperMessage();
                operMessage.setCreatetime(new Date());
                operMessage.setDelflag("1");
                operMessage.setStatus("1");
                if (StringUtils.isBlank(skuProductVo.getStaff1Id())) {
                    operMessage.setSupplyid(supply.getSupplyId());
                    operMessage.setSkuproductid(Long.valueOf(skuProductVo.getId()));
                    operMessage.setType(OperMessage.TYPE_SELLER_PUBLISH_PRODUCT);
                    operMessage.setSource(OperMessage.RESOURCE_SELLER);
                }else{
                    operMessage.setSalespersonid(skuProductVo.getStaff1Id());
                    operMessage.setSupplyid(supply.getSupplyId());
                    operMessage.setSkuproductid(Long.valueOf(skuProductVo.getId()));
                    operMessage.setType(OperMessage.TYPE_SELLER_PUBLISH_PRODUCT);
                    operMessage.setSource(OperMessage.RESOURCE_STAFF);
                }
                operMessage.setIsread("1");
                operMessageService.insertSelective(operMessage);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return BaseResult.success(bs);
        } catch (Exception e) {
            return BaseResult.exception(ErrorCode.SQLExceptionRetInfo);
        }

    }

    /**
     * @Title: skuProductEdit
     * @Description: 商品编辑
     * @author: guoconglin
     * @param request
     * @param skuProductVo
     * @return
     * @date: 2016年8月12日 上午9:10:56
     * @return: BaseResult
     */
    @RequestMapping(value = {"/skuProductEdit"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult skuProductEdit(HttpServletRequest request,
                    SkuProductVo skuProductVo) {
        logger.info("SkuProductRestController skuProductEdit begin.");
        logger.info("SkuProductRestController skuProductEdit:" + JSON.toJSONString(skuProductVo));
        try {
            logger.info("skuProductEdit begin.");
            BaseResult baseResult = skuProductService.updateById(skuProductVo);
            logger.info("skuProductEdit baseResult:" + JSON.toJSONString(baseResult));
            logger.info("skuProductEdit end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("skuProductEdit error:" + e.getMessage());
            logger.info("SkuProductRestController skuProductEdit end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: selectByCateId
     * @Description: 查找二级分类下的属性
     * @author: guoconglin
     * @param request
     * @param psVo
     * @return
     * @date: 2016年8月16日 上午11:02:40
     * @return: BaseResult
     */
    @RequestMapping(value = "/selectByCateId", method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody BaseResult selectByCateId(HttpServletRequest request, ProductCateVo psVo) {
        logger.info("SkuProductRestController selectByCateId begin.");
        logger.info("SkuProductRestController selectByCateId cateId:"
                        + JSON.toJSONString(psVo.getCateId()));
        try {
            BaseResult baseResult = productSpecService.selectByCateId(psVo.getCateId());
            logger.info("selectByCateId end.");
            logger.info("SkuProductRestController selectByCateId end.");
            return baseResult;
        } catch (Exception e) {
            logger.error("SkuProductRestController selectByCateId error:" + e.getMessage());
            logger.error("SkuProductRestController selectByCateId end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: getProductNameByCateId
     * @Description: 查找二级分类下的商品名
     * @author: guoconglin
     * @param request
     * @param psVo
     * @return
     * @date: 2016年8月16日 上午11:02:33
     * @return: BaseResult
     */
    @RequestMapping(value = "/getProductNameByCateId",
                    method = {RequestMethod.GET, RequestMethod.POST})
    public @ResponseBody BaseResult getProductNameByCateId(HttpServletRequest request,
                    ProductCateVo psVo) {
        logger.info("SkuProductRestController getProductNameByCateId begin.");
        logger.info("SkuProductRestController getProductNameByCateId cateId:"
                        + JSON.toJSONString(psVo.getCateId()));
        try {
            BaseResult baseResult = productCateService.selectByCateId(psVo.getCateId());
            logger.info("getProductNameByCateId end.");
            logger.info("SkuProductRestController getProductNameByCateId end.");
            return baseResult;
        } catch (Exception e) {
            logger.error("SkuProductRestController getProductNameByCateId error:" + e.getMessage());
            logger.error("SkuProductRestController getProductNameByCateId end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: test
     * @Description: 查询区域
     * @author: guoconglin
     * @return
     * @date: 2016年8月16日 下午3:34:58
     * @return: BaseResult
     */
    @RequestMapping(value = {"/initAreaAll"}, method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody BaseResult initAreaAll(String userId) {
        logger.info("initAreaAll begin");
       /* BaseResult baseResult = adminAreaService.selectAreaAll();*/
       /* BaseResult baseResult = supplyAreaService.selectSupplyArea();*/
        BaseResult baseResult = supplyService.selectByUsersId(userId);
        Supply s = (Supply) baseResult.getData();
        BaseResult bs = null;
        if(s.getSupplyId() != null){
            String supplyId = String.valueOf(s.getSupplyId());
            bs = supplyAreaService.selectSupplyArea(supplyId);
        }
        logger.info("initAreaAll end");
        return BaseResult.success(bs);
    }

    /**
     * 
     * @Title: initAreaAll
     * @Description: 
     * @author: guoconglin
     * @return
     * @date: 2016年8月16日 下午3:34:58
     * @return: BaseResult
     */
    @RequestMapping(value = {"/selectBuyerId"}, method = {RequestMethod.POST, RequestMethod.GET})
    public @ResponseBody BaseResult selectBuyerId(HttpServletRequest request, String mobile) {
        logger.info("selectBuyerId begin");
        /* String mobile = request.getParameter("mobile"); */
        BaseResult baseResult = frontUserService.selectMobilAndId(mobile);
        logger.info("selectBuyerId end");
        return baseResult;
    }

    /**
     * @Title: updateTime
     * @Description: 跟新时间
     * @author: guoconglin
     * @param request
     * @param skuProductVo
     * @return
     * @date: 2016年8月16日 下午4:29:57
     * @return: BaseResult
     */
    @RequestMapping(value = {"/updateTime"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult updateTime(HttpServletRequest request,
                    SkuProductVo skuProductVo) {
        logger.info("SkuProductRestController updateTime begin.");
        logger.info("SkuProductRestController updateTime:" + JSON.toJSONString(skuProductVo));
        try {
            logger.info("updateTime begin.");
            BaseResult baseResult = skuProductService.updateTime(skuProductVo);
            logger.info("updateTime baseResult:" + JSON.toJSONString(baseResult));
            logger.info("updateTime end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("updateTime error:" + e.getMessage());
            logger.info("SkuProductRestController updateTime end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    @RequestMapping(value = {"/uploadPic"}, method = {RequestMethod.POST})
    public @ResponseBody BaseResult uploadPic(HttpServletRequest request) throws Exception {
        logger.info("SkuProductRestController add begin.");
        MultipartHttpServletRequest re = (MultipartHttpServletRequest) request;
        MultipartFile file = re.getFile(request.getParameter("id"));
        if (file != null) {
            String path = fileUpload.getPath(file, request, "/product/");
            return BaseResult.success(path);
        }
        return BaseResult.success();
    }

    /**
     * @Title: changeStatusSwitchs
     * @Description: 下架
     * @author: guoconglin
     * @param request
     * @return
     * @date: 2016年8月9日 下午8:11:59
     * @return: BaseResult
     */
    @RequestMapping(value = {"/changeStatusSwitchs"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult changeStatusSwitchs(HttpServletRequest request) {
        logger.info("SkuProductRestController changeStatusSwitchs begin.");
        try {
            logger.info("changeStatusSwitchs begin.");
            BaseResult baseResult =
                            skuProductService.updateSkuProductStatus(request.getParameter("id"));
            logger.info("changeStatusSwitchs baseResult:" + JSON.toJSONString(baseResult));
            logger.info("changeStatusSwitchs end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("changeStatusSwitchs error:" + e.getMessage());
            logger.info("SkuProductRestController changeStatusSwitchs end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    /**
     * @Title: changeStatusSwitch
     * @Description: 上架
     * @author: guoconglin
     * @param request
     * @return
     * @date: 2016年8月11日 上午10:39:47
     * @return: BaseResult
     */
    @RequestMapping(value = {"/changeStatusSwitch"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult changeStatusSwitch(HttpServletRequest request) {
        logger.info("SkuProductRestController changeStatusSwitch begin.");
        try {
            logger.info("changeStatusSwitch begin.");
            BaseResult baseResult = skuProductService.updateSkuProduct(request.getParameter("ids"));
            logger.info("changeStatusSwitch baseResult:" + JSON.toJSONString(baseResult));
            logger.info("changeStatusSwitch end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("changeStatusSwitch error:" + e.getMessage());
            logger.info("SkuProductRestController changeStatusSwitch end.");
            return BaseResult.exception(e.getMessage());
        }
    }
    @RequestMapping(value={"getOverPlusNumBySkuId"},method=RequestMethod.GET)
    @ResponseBody
    public BaseResult getOverPlusNumBySkuId(String id){
        logger.info("SkuProductRestController getOverPlusNumBySkuId begin.");
        try {
            logger.info("getOverPlusNumBySkuId begin.");
            BaseResult baseResult = skuProductService.getOverPlusNumBySkuId(id);
            logger.info("getOverPlusNumBySkuId baseResult:" + JSON.toJSONString(baseResult));
            logger.info("getOverPlusNumBySkuId end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("getOverPlusNumBySkuId error:" + e.getMessage());
            logger.info("SkuProductRestController getOverPlusNumBySkuId end.");
            return BaseResult.exception(e.getMessage());
        }
    }
    /**
     * @Title: updateOverPlusNumBySkuId 
     * @Description: 更新库存
     * @author: Administrator
     * @param overPlusNum
     * @param id
     * @return
     * @date: 2016年9月12日 上午9:49:07 
     * @return: BaseResult
     */
    @RequestMapping(value={"updateOverPlusNumBySkuId"},method=RequestMethod.GET)
    @ResponseBody
    public BaseResult updateOverPlusNumBySkuId(double overPlusNum, String id){
        logger.info("SkuProductRestController updateOverPlusNumBySkuId begin.");
        try {
            logger.info("updateOverPlusNumBySkuId begin.");
            BaseResult baseResult = skuProductService.updateOverPlusNumBySkuId(overPlusNum, id);
            logger.info("updateOverPlusNumBySkuId baseResult:" + JSON.toJSONString(baseResult));
            logger.info("updateOverPlusNumBySkuId end.");
            return baseResult;

        } catch (Exception e) {
            logger.error("updateOverPlusNumBySkuId error:" + e.getMessage());
            logger.info("SkuProductRestController updateOverPlusNumBySkuId end.");
            return BaseResult.exception(e.getMessage());
        }
    }
    @RequestMapping(value="/getAreaNameByAreaId", method={RequestMethod.GET})
    public @ResponseBody BaseResult getAreaNameByAreaId(String areaId){
        logger.info("getAreaNameByAreaId begin");
        return BaseResult.success(adminAreaService.getAreaNameByAreaId(areaId));
    }
    
    
    /**
     */
    @RequestMapping(value = {"/saveProduct"}, method = RequestMethod.POST)
    @ResponseBody
    public BaseResult saveProduct(ProductVo productVo) {
    	
    	return null;
    	
    }
}
